package com.mythesis.bpel.ui.client.property;


import java.util.ArrayList;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

import com.gwtext.client.core.NameValuePair;
import com.gwtext.client.widgets.grid.GridView;
import com.gwtext.client.widgets.grid.PropertyGridPanel;


public class Property extends Composite{	
	/**
	 * grid contain properties
	 */
	protected PropertyGridPanel gridPanel;
	
	/**
	 * structure of xml string must have the format like this
	 * <Properties>
	 * 		<Property>
	 * 			<name>Name</name> 						<!-- name is a String -->
	 * 			<type>2</type>							<!-- type is a interger: 1 indicate to BOOLEAN TYPE; 2 indicate to STRING TYPE
	 * 			<default_value>Start</default_value>	<!-- default_value is: String if type is 2; 0 or 1 if type is 1
	 * 		</Property>;
	 * 		<Property>
	 * 			<name>Documentation</name>
	 * 			<type>2</type>
	 * 			<default_value>nothing</default_value>
	 * 		</Property>;
	 * 		<Property>
	 * 			<name>Create Instances</name>
	 * 			<type>1</type>
	 * 			<default_value>1</default_value>
	 * 		</Property>
	 * </Properties>;
	 * @param xml: xml string specify control's property
	 */
	public Property(String xml){
		gridPanel = new PropertyGridPanel();
		
		gridPanel.setSorted(false);
		
		GridView view = new GridView();  
	      view.setForceFit(true);  
	        view.setScrollOffset(2);    
		gridPanel.setView(view);  
         
		
		ArrayList arr = PropertiesFactory.parseXML(xml);
		int size = arr.size();
		NameValuePair[] source = new NameValuePair[size];
		for(int i = 0;i<size;i++){
			String[] property = (String[])arr.get(i);
			switch(Integer.valueOf(property[1])){
				case 1://Boolean
					switch(Integer.valueOf(property[2])){
						case 0://true
							source[i] = new NameValuePair(property[0],Boolean.FALSE);
							break;
						case 1://false
							source[i] = new NameValuePair(property[0],Boolean.TRUE);
							break;
						default: source[i] = new NameValuePair(property[0],Boolean.TRUE);
							break;
					}
					break;
				case 2: //String
					source[i] = new NameValuePair(property[0],property[2]);
					break;
				default: source[i] = new NameValuePair(property[0],property[2]);
					break;
			}			
		}		
		
		gridPanel.setSource(source);
		gridPanel.setAutoHeight(true);
		gridPanel.setTitle("Properties");
		
		initWidget(gridPanel);		
		gridPanel.setWidth(300);
		
	}
	
	
	/*
	protected SimpleGrid gridPanel;
	public Property(String n,String d){
		name = n;
		d = documentation;
	}
	*/
}
